package org.example.mydeom.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.mydeom.pojo.dto.RoleForUserDTO;
import org.example.mydeom.pojo.entity.Role;
import org.example.mydeom.result.Result;
import org.example.mydeom.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
@Slf4j
@Tag(name = "角色管理",description = "角色相关接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping()
    @Operation(summary = "添加角色",description = "添加角色")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result<Void> addRole(@RequestBody Role role){
        log.info("添加角色：{}",role);
        return  roleService.add(role);
    }

    @PostMapping("/addRoleForUser")
    @Operation(summary = "为用户添加角色",description = "为用户添加角色")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result<Void> addRoleForUser(@RequestBody RoleForUserDTO roleForUser){
        log.info("为用户添加角色：{}",roleForUser);
        return  roleService.addRoleForUser(roleForUser);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色",description = "删除角色")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MIDADMIN')")
    public Result<Void> deleteRole(@PathVariable Long id){
        log.info("删除角色,角色id：{}",id);
        roleService.delById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询角色",description = "查询角色")
    public Result<Role> getRole(@PathVariable Long id){
        log.info("查询角色,角色id：{}",id);
        return roleService.findById(id);
    }

}
